package org.javaboy.thymeleaf.controller;

import org.javaboy.thymeleaf.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 江南一点雨
 * @Date 2019-05-28 14:42
 */
@Controller
public class IndexController {
    @GetMapping("page1")
    public String page1(Model model) {
        List<User> users = new ArrayList<>();
        for (int i = 1; i <=10; i++) {
            User u = new User();
            u.setId((long) i);
            u.setName("javaboy:" + i);
            u.setAddress("深圳:" + i);
            u.setAddTime(LocalDateTime.now());
            users.add(u);
        }
        model.addAttribute("users", users);
        model.addAttribute("username", "李四");
        return "page1";
    }

    @GetMapping("page2")
    public String page2(Model model) {
        List<User> users = new ArrayList<>();
        for (int i = 1; i <=10; i++) {
            User u = new User();
            u.setId((long) i);
            u.setName("javaboy:" + i);
            u.setAddress("深圳:" + i);
            u.setAddTime(LocalDateTime.now());
            users.add(u);
        }
        model.addAttribute("users", users);
        model.addAttribute("username", "王五");

        return "page2";
    }

    @GetMapping("page3")
    public String page3(@RequestParam int n, Model model) {
        List<User> users = new ArrayList<>();
        for (int i = 1; i <=10; i++) {
            User u = new User();
            u.setId((long) i);
            u.setName("javaboy:" + i);
            u.setAddress("深圳:" + i);
            u.setAddTime(LocalDateTime.now());
            users.add(u);
        }
        model.addAttribute("users", users);
        model.addAttribute("username", "赵六");

        String url="page1";
        if(n>10)
            url="page2";

        return url;
    }
}
